﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace GridSplitterDemo
{
    public partial class MainWindow : Window
    {
        private const int MaxDepth = 3; // 设置最大递归层数

        public MainWindow()
        {
            InitializeComponent();
            SetContextMenuForGrid(MainGrid); // 为初始的 Grid 设置右键菜单
        }

        // 右键菜单打开事件
        private void Grid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            var grid = sender as Grid;
            if (grid == null) return;
        }

        // 为 Grid 设置右键菜单
        private void SetContextMenuForGrid(Grid grid)
        {
            var contextMenu = new ContextMenu();
            var horizontalSplitItem = new MenuItem { Header = "水平分割" };
            var verticalSplitItem = new MenuItem { Header = "垂直分割" };

            horizontalSplitItem.Click += (s, e) => OnHorizontalSplitClick(s, e, grid);
            verticalSplitItem.Click += (s, e) => OnVerticalSplitClick(s, e, grid);

            contextMenu.Items.Add(horizontalSplitItem);
            contextMenu.Items.Add(verticalSplitItem);

            grid.ContextMenu = contextMenu;
        }

        // 获取当前 Grid 的层级
        private int GetGridDepth(Grid grid)
        {
            if (grid.Tag is int depth)
                return depth;
            if (grid.Tag is string depthStr && int.TryParse(depthStr, out var parsedDepth))
                return parsedDepth;
            return 0; // 默认深度为 0
        }

        // 水平分割
        private void OnHorizontalSplitClick(object sender, RoutedEventArgs e, Grid grid)
        {
            int currentDepth = GetGridDepth(grid);
            if (currentDepth >= MaxDepth)
            {
                MessageBox.Show("已达到最大递归层数，无法继续分割！");
                return;
            }
            // 添加行定义
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(5, GridUnitType.Pixel) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });

            // 创建子 Grid
            var gridTop = CreateSubGrid(currentDepth + 1);
            var gridBottom = CreateSubGrid(currentDepth + 1);

            // 创建分割线
            var splitter = new GridSplitter
            {
                Height = 5,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Center,
                ResizeDirection = GridResizeDirection.Rows,
                Background = new SolidColorBrush(Colors.Gray) // 设置背景颜色为灰色
            };

            // 设置 ZIndex，确保分割线在最上层
            Panel.SetZIndex(splitter, 1);
            Panel.SetZIndex(gridTop, 0);
            Panel.SetZIndex(gridBottom, 0);

            // 添加到父 Grid
            grid.Children.Add(gridTop);
            grid.Children.Add(splitter);
            grid.Children.Add(gridBottom);

            // 设置子元素的位置
            Grid.SetRow(gridTop, 0);
            Grid.SetRow(splitter, 1);
            Grid.SetRow(gridBottom, 2);

            grid.UpdateLayout(); // 强制刷新布局
        }


        // 垂直分割
        private void OnVerticalSplitClick(object sender, RoutedEventArgs e, Grid grid)
        {
            int currentDepth = GetGridDepth(grid);
            if (currentDepth >= MaxDepth)
            {
                MessageBox.Show("已达到最大递归层数，无法继续分割！");
                return;
            }
            // 添加列定义
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

            // 创建子 Grid
            var gridLeft = CreateSubGrid(currentDepth + 1);
            var gridRight = CreateSubGrid(currentDepth + 1);

            // 创建分割线
            var splitter = new GridSplitter
            {
                Width = 5,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Stretch,
                ResizeDirection = GridResizeDirection.Columns,
                Background = new SolidColorBrush(Colors.Gray) // 设置背景颜色为灰色
            };

            // 设置 ZIndex，确保分割线在最上层
            Panel.SetZIndex(splitter, 1);
            Panel.SetZIndex(gridLeft, 0);
            Panel.SetZIndex(gridRight, 0);

            // 添加到父 Grid
            grid.Children.Add(gridLeft);
            grid.Children.Add(splitter);
            grid.Children.Add(gridRight);

            // 设置子元素的位置
            Grid.SetColumn(gridLeft, 0);
            Grid.SetColumn(splitter, 1);
            Grid.SetColumn(gridRight, 2);

            grid.UpdateLayout(); // 强制刷新布局
        }


        // 创建子 Grid 并设置属性
        private Grid CreateSubGrid(int depth)
        {
            var grid = new Grid
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                Tag = depth // 设置递归层数
            };

            // 设置背景颜色，根据深度调整颜色
            if (depth == 1)
                grid.Background = new SolidColorBrush(Colors.LightBlue);
            else if (depth == 2)
                grid.Background = new SolidColorBrush(Colors.LightGreen);
            else if (depth == 3)
                grid.Background = new SolidColorBrush(Colors.LightYellow);
            else
                grid.Background = new SolidColorBrush(Colors.LightGray);

            SetContextMenuForGrid(grid); // 为子 Grid 设置右键菜单
            return grid;
        }

    }
}
